package com.wwwarehouse.resource_center.fragment.resource_efficiency_evaluation;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.SearRulesDeskPagersAdapter;
import com.wwwarehouse.resource_center.bean.goods.ResourceEvaluationBean;
import com.wwwarehouse.resource_center.bean.goods.SerializableMap;
import com.wwwarehouse.resource_center.bean.goods.WebDataBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.Event;
import com.wwwarehouse.resource_center.eventbus_event.HideTitleBean;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;
import com.wwwarehouse.resource_center.utils.EventBusUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/10/28.
 *         <p/>
 *         资源效率评估子页面
 */

public class ResourceEvaluationChildListFragment extends TheParentFragment implements SearRulesDeskPagersAdapter.OnItemClickListener {

    private RecyclerView mRvContent = null;
    private SearRulesDeskPagersAdapter mAdapter;
    public static final int RESOURCE_EFFICIENCY_EVALUATION = 1;
    private int mHeight;
    private WebDataBean dataBean;
    private String type;
    private String mBaseUrl = "";

    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_desk_recycler_pagers;
    }

    @Override
    protected void init(View view) {
        mRvContent = (RecyclerView) view.findViewById(R.id.rv_content);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ResourceEvaluationChildListFragment)
            mActivity.setTitle(getString(R.string.resource_efficiency_evaluation));
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            SerializableMap myMap = (SerializableMap) bundle.getSerializable("map");
            dataBean = bundle.getParcelable("buId");
            Map<String, Object> map = myMap.getMap();
            int pos = bundle.getInt("pos");
            type = bundle.getString("type");
            map.put("page", pos);
            httpPost(ResourceConstant.RESOURCE_EFFICIENCY_EVALUATION, map, RESOURCE_EFFICIENCY_EVALUATION, true, "");
        }
        mRvContent.addItemDecoration(new SpaceItemDecoration(10, 10));
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3));
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                mHeight = mRvContent.getHeight();
            }
        });
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case RESOURCE_EFFICIENCY_EVALUATION:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    ResourceEvaluationBean evaluationBean = JSON.parseObject(commonClass.getData().toString(), ResourceEvaluationBean.class);
                    List<ResourceEvaluationBean.ListBean> listBeen = evaluationBean.getList();
                    mAdapter = new SearRulesDeskPagersAdapter(mActivity, listBeen, mHeight);
                    mRvContent.setAdapter(mAdapter);
                    mAdapter.setOnItemClickListener(this);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void ItemClickListener(int position, View view, String data) {
        EventBusUtil.sendEvent(new Event(HideTitleBean.CODE.A, new HideTitleBean()));
        ((BaseCardDeskActivity) mActivity).recoveryTitleBtStatus_4();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        if ("spu".equals(type)) {
            List<String> categoryUkid = new ArrayList<>();
            categoryUkid.add(data);
            dataBean.setCategoryUkid(categoryUkid);
            dataBean.setItemUkid(new ArrayList<String>());
        } else {
            dataBean.setCategoryUkid(new ArrayList<String>());
            List<String> ItemUkid = new ArrayList<>();
            ItemUkid.add(data);
            dataBean.setItemUkid(ItemUkid);
        }
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));
        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = "";
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_RELEASE;
        }

        String newUrl = mBaseUrl + ResourceConstant.RESOURCE_EFFICIENCY;
        Bundle bundle = new Bundle();
        bundle.putString("url", newUrl);
        bundle.putString("title", getString(R.string.res_evaluation));
        bundle.putString("data", JSON.toJSONString(dataBean));
        ResourceEfficiencyWebViewFragment commonBridgeWebViewFragment = new ResourceEfficiencyWebViewFragment();
        commonBridgeWebViewFragment.setArguments(bundle);
        pushFragment(commonBridgeWebViewFragment);
    }
}
